package com.probiz.estore.system.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.system.Region;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.system.dao.RegionDao;

/**
 * Region Data Access Object (DAO) implementation. Developer introduced
 * interfaces should be declared here. Won't get overwritten.
 */
public class RegionDaoImpl extends HibernateGenericDaoImpl<Region> implements
		RegionDao {
	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "select s from Region s ");
	}

	/**
	 * 通过一系列名字找到相应的地区。<br />
	 * 比如输入[中国，广东，广州]，返回3条Region记录组成的List
	 * 
	 * @param countryName
	 *            国家
	 * @param stateName
	 *            省
	 * @param cityName
	 *            市
	 * @return
	 */
	public Region[] findMatchRegions(String countryName, String stateName,
			String cityName) {
		Region[] regionArray = new Region[3];
		if (StringUtils.isNotEmpty(countryName)
				&& StringUtils.isNotEmpty(stateName)
				&& StringUtils.isNotEmpty(cityName)) {// 三者非空
			StringBuffer sql = new StringBuffer("from Region r3")
					.append(" where r3.regionName = '")
					.append(cityName)
					.append("'")
					.append(" and r3.parentRegionId in (")
					.append(
							"select r2.regionId from Region r2 where r2.regionName ="
									+ "'"
									+ stateName
									+ "' and r2.parentRegionId in ("
									+ "select r1.regionId from Region r1 where r1.regionName ='"
									+ countryName + "'" + ")").append(")");
			List<Region> cities = super.findByHql(sql.toString());
			if (cities.size() > 0) {
				regionArray[2] = cities.get(0);
				regionArray[1] = this.getById(regionArray[2]
						.getParentId());
				if (regionArray[1] != null) {
					regionArray[0] = this.getById(regionArray[1]
							.getParentId());
				}
			}
		} else if (StringUtils.isNotEmpty(countryName)
				&& StringUtils.isNotEmpty(stateName)
				&& StringUtils.isEmpty(cityName)) {// 二者非空
			StringBuffer sql = new StringBuffer("from Region r2").append(
					" where r2.regionName = '").append(stateName).append("'")
					.append(" and r2.parentRegionId in (").append(
							"select r1.regionId from Region r1 where r1.regionName ="
									+ "'").append(countryName).append("'")
					.append(")");
			System.out.println("sql2=【" + sql + "】");
			List<Region> states = this.findByHql(sql.toString());
			if (states.size() > 0) {
				regionArray[1] = states.get(0);
				regionArray[0] = this.getById(regionArray[1]
						.getParentId());
			}
		} else if (StringUtils.isNotEmpty(countryName)
				&& StringUtils.isEmpty(stateName)
				&& StringUtils.isEmpty(cityName)) {// 国家非空
			StringBuffer sql = new StringBuffer("from Region r1").append(
					" where r1.regionName = '").append(countryName).append("'");
			System.out.println("sql2=【" + sql + "】");
			List<Region> countries = this.findByHql(sql.toString());
			if (countries.size() > 0) {
				regionArray[0] = countries.get(0);
			}
		}
		return regionArray;
	}



	
}